#include "the_video_clip.h"
#include<qdebug.h>
#include<QCoreApplication>

The_video_clip::The_video_clip(QObject *parent) : QThread(parent)
{


}

QStringList The_video_clip::getsettinginfo()
{
    QString setpath= QCoreApplication::applicationDirPath()+"/"+"data";//绝对路径
    QString setinfo=setpath+"/"+"配置信息.txt";
    QFile file(setinfo);

    QString info;
    if(file.open(QIODevice::ReadOnly))
    {
        info=file.readAll();

        file.close();
    }

    QStringList infolist=info.split(QRegExp("[{}]"));

    for(int i=0;i<infolist.count();i++)
        qDebug()<<i<<infolist[i];
    return infolist;

}


void The_video_clip::function(QStringList videofilelist, QString outpath,QTime cuttime, QTime onevideotime, QStringList audiolist, QString zimufile, QString titie,QString logo, bool addaudio, bool addzimu)
{

    //获取参数
    QStringList settinginfo=getsettinginfo();

    //qDebug()<<"void The_video_clip::function" <<logo;

    ffmpeg_mixing_shear mixing_shear;
    connect(&mixing_shear,&ffmpeg_mixing_shear::signal_infoout,this,&The_video_clip::slot_sending);

    if(settinginfo[7]=="y")
    {
        valuepro="正在裁剪开头结尾...";

        mixing_shear.video_tailor(videofilelist,settinginfo[3].toInt(),settinginfo[5].toInt(),outpath);
        emit signal_infoout("视频制作完成...","视频制作完成...");
        return;
    }


    QStringList filelist=videofilelist;


    //编码
    int btlu=4000,fenbianw=1280,fenbianh=720;
    if(settinginfo[17]=="y")
    {
        btlu=settinginfo[19].toInt();
        fenbianw=settinginfo[21].toUInt();
        fenbianh=settinginfo[23].toUInt();
    }
    valuepro="正在编码...";
    filelist=
    mixing_shear.video_coding_mp4(filelist,btlu,fenbianw,fenbianh, outpath);


    //裁剪开头结尾
    if(settinginfo[1]=="y")
    {
        valuepro="正在裁剪开头结尾...";
        filelist=
        mixing_shear.video_tailor(filelist,settinginfo[3].toInt(),settinginfo[5].toInt(),outpath);
        mixing_shear.DeleteFileOrFolder_coding_mp4();
    }

    //视频静音
    valuepro="正在静音...";
    filelist=
    mixing_shear.Video_mute(filelist,outpath);
    mixing_shear.DeleteFileOrFolder_coding_mp4();
    if(settinginfo[1]=="y")
    {
        qDebug()<<"mixing_shear.DeleteFileOrFolder_tailor()";
        mixing_shear.DeleteFileOrFolder_tailor();
    }
    //画面裁剪
    if(settinginfo[25]=="y")
    {
        valuepro="正在画面裁剪...";
        filelist=
        mixing_shear.video_picture_cut(filelist,settinginfo[27].toUInt(),settinginfo[29].toUInt(),settinginfo[31].toUInt(),settinginfo[33].toUInt(),outpath);
    }

    //切割
    valuepro="正在切割...";
    filelist=
    mixing_shear.video_cut(filelist,cuttime,outpath);
    mixing_shear.DeleteFileOrFolder_mute();//删除静音文件加
    if(settinginfo[25]=="y")
    {
        mixing_shear.DeleteFileOrFolder_piccut();
    }

    //编码
//    int btlu=4000,fenbianw=1280,fenbianh=720;
    if(settinginfo[17]=="y")
    {
        btlu=settinginfo[19].toInt();
        fenbianw=settinginfo[21].toUInt();
        fenbianh=settinginfo[23].toUInt();
    }
    if(settinginfo[25]=="y")
    {
        fenbianw=settinginfo[27].toUInt();//w
        fenbianh=settinginfo[29].toUInt();//h
    }

    valuepro="正在编码...";
    filelist=
    mixing_shear.video_coding_ts(filelist,btlu,fenbianw,fenbianh, outpath);
    mixing_shear.DeleteFileOrFolder_cut();//删除分段文件
    //合并
    valuepro="正在合并...";
    QString video_hebin=
    mixing_shear.video_merge(filelist,outpath);
    mixing_shear.DeleteFileOrFolder_coding_ts();


    QString onlyvideo=video_hebin; //这样更好,

    //添加水印

    if(logo.isEmpty()==false)
    {
        int fontsize=25,x=20,y=20;
        if(settinginfo[9]=="y")
        {
            fontsize=settinginfo[11].toInt();
            x=settinginfo[13].toUInt();
            y=settinginfo[15].toUInt();
        }
        valuepro="正在添加水印...";
        onlyvideo=
        mixing_shear.addsuiyin(onlyvideo,logo,fontsize,x,y, outpath);
        mixing_shear.DeleteFileOrFolder_merge();
    }

    //添加音频

    if(addaudio==true)
    {
        valuepro="正在添加音频...";
        onlyvideo=
        mixing_shear.addaudio(onlyvideo,audiolist,outpath);
        mixing_shear.DeleteFileOrFolder_merge();

    }

    //添加字幕

    QString addzimu_video;
    if(addzimu==true)
    {
        valuepro="正在添加字幕...";
        onlyvideo=
        mixing_shear.addzimufile(onlyvideo,zimufile,outpath);
        mixing_shear.DeleteFileOrFolder_merge();

    }


    //分割成想要的长度
    valuepro="正在重新分割视频...";
    //文件名
    QString videofilename=outpath+"/"+titie+".mp4";

    mixing_shear.video_cut_new(onlyvideo,onevideotime,videofilename);

//    mixing_shear.DeleteFileOrFolder_mute();//删除静音文件加
//    mixing_shear.DeleteFileOrFolder_cut();//删除分段文件
      mixing_shear.DeleteFileOrFolder_merge();

    if(addzimu)
    {
        mixing_shear.DeleteFileOrFolder_addzimu();
    }
    if(addaudio)
         mixing_shear.DeleteFileOrFolder_addaudio();

    if(logo.isEmpty()==false)
        mixing_shear.DeleteFileOrFolder_suiyin();

    emit signal_infoout("视频制作完成...","视频制作完成...");
    return;

}

void The_video_clip::slot_sending(QString str, QString pro)
{
     emit signal_infoout(str,valuepro);
}
